import { _decorator, Button, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { ItemStatusEnum, ItemTypeEnum } from '../Enum';
import DataManger from '../RunTime/DataManager';
import { RendorManager } from '../Base/RendorManager';
import { ItemManager } from '../Item/ItemManager';
const { ccclass, property } = _decorator;

@ccclass('InventoryManager')
export class InventoryManager extends RendorManager {
    @property(Label)
    label: Label = null
    
    @property(Button)
    leftBtn: Button = null

    @property(Button)
    rightBtn: Button = null

    @property(Node)
    placeHoder: Node = null

    @property(Node)
    hand: Node = null

    @property(Prefab)
    keyPF: Prefab = null

    @property(Prefab)
    mailPF: Prefab = null

    
    rendor(): void {
        this.placeHoder.destroyAllChildren()
        const isInventoryItems = DataManger.Instance.items.filter(i => i.status === ItemStatusEnum.Inventory)
        if (isInventoryItems.length > 0) {
            this.node.active = true
        } else {
            this.node.active = false
        }


        if (isInventoryItems.length) {
            if (DataManger.Instance.curItemType) {
                const item = DataManger.Instance.items.find(i => i.type === DataManger.Instance.curItemType)
                if (item.status === ItemStatusEnum.Inventory) {
                    this.generateItem(DataManger.Instance.curItemType) 
                } else {
                    const type = isInventoryItems[0].type
                    this.generateItem(type)                 
                    DataManger.Instance.curItemType = type
                }
            } else {
                const type = isInventoryItems[0].type
                //ui显示
                this.generateItem(type)
                //更新数据
                DataManger.Instance.curItemType = type
            }
        }

        this.hand.active = Boolean(DataManger.Instance.curItemType) && DataManger.Instance.isSelect
        this.changeBtnInteractable()
    }

    /** 在背包中显示出curItem的ui */
    generateItem(type:ItemTypeEnum) {
        switch (type) {
            case ItemTypeEnum.Key:
                const addnode = instantiate(this.keyPF)
                this.placeHoder.addChild(addnode)
                this.label.string = addnode.getComponent(ItemManager).label
                break;
            case ItemTypeEnum.Mail:
                const addnode2 = instantiate(this.mailPF)
                this.placeHoder.addChild(addnode2)
                this.label.string = addnode2.getComponent(ItemManager).label
                break;
            default: break;
        }
    }

    /** 选中背包道具 */
    handSelect() {
        DataManger.Instance.isSelect = !DataManger.Instance.isSelect
    }

    leftButton() {
        if (DataManger.Instance.curItemType === null) {
            return
        }
        const isInventoryItems = DataManger.Instance.items.filter(i => i.status === ItemStatusEnum.Inventory)
        const index = isInventoryItems.findIndex(i => i.type === DataManger.Instance.curItemType)
        if (index > 0) {
            DataManger.Instance.isSelect = false
            DataManger.Instance.curItemType = isInventoryItems[index - 1].type
        }
    }

    rightButton() {
        if (DataManger.Instance.curItemType === null) {
            return
        }
        const isInventoryItems = DataManger.Instance.items.filter(i => i.status === ItemStatusEnum.Inventory)
        const index = isInventoryItems.findIndex(i => i.type === DataManger.Instance.curItemType)
        if (index < isInventoryItems.length - 1) {
            DataManger.Instance.isSelect = false
            DataManger.Instance.curItemType = isInventoryItems[index + 1].type
        }
    }

    changeBtnInteractable() {
        if (DataManger.Instance.curItemType === null) {
            return
        }
        const isInventoryItems = DataManger.Instance.items.filter(i => i.status === ItemStatusEnum.Inventory)
        const index = isInventoryItems.findIndex(i => i.type === DataManger.Instance.curItemType)
        this.leftBtn.interactable = index > 0
        this.rightBtn.interactable = index < isInventoryItems.length - 1

    }


    
}


